package com.si.anddos;

import java.net.MalformedURLException;
import java.util.Date;

import com.si.anddos.dpadapters.AnddosDbAdapter;
import com.si.anddos.dpadapters.Account;
import com.si.anddos.library.main.AccountDetail;
import com.si.anddos.library.main.factory.ServiceProviderFactory;
import com.si.anddos.library.provider.ServiceProvider;
import com.si.anddos.library.provider.ServiceProviderException;

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class AccountDetailActivity extends OptionsMenuActivity {
	private TextView textServiceDetail;
	private TextView textUsernameDetail;
	private TextView textEmailDetail;
	private TextView textBilledUntilDetail;
	private TextView textCurrentSpaceDetail;

	private long accountId;
	private AnddosDbAdapter dbAdapter;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);      
        setContentView(R.layout.account_detail);
        setTitle(R.string.titleDetailAccount);
          
        Bundle extras = getIntent().getExtras();
        accountId = extras != null ? extras.getLong(AnddosDbAdapter.colAccountID) : null;
        
        textServiceDetail = (TextView) findViewById(R.id.textServiceDetail);
        textUsernameDetail = (TextView) findViewById(R.id.textUsernameDetail);
        textEmailDetail = (TextView) findViewById(R.id.textEmailDetail);
        textBilledUntilDetail = (TextView) findViewById(R.id.textBilledUntilDetail);
        textCurrentSpaceDetail = (TextView) findViewById(R.id.textCurrentSpaceDetail);

        dbAdapter = new AnddosDbAdapter(this);
        
        
	}
	
	 // nahrada za tlacitko hide
    @Override
	public void onBackPressed() {
		setResult(RESULT_OK);
		finish();
	}
    
    @Override
	protected void onResume() {
		super.onResume();
		populateAccount();
	}


	private void populateAccount() {
    	// populate data from database to account detail activity
    	
    	//ProgressDialog dialog = ProgressDialog.show(this, "", getText(R.string.loadingAccountDetail), true);	
		dbAdapter.open();   	
    	
        Cursor account = dbAdapter.getAccount(accountId);
        startManagingCursor(account);
        String username = account.getString(account.getColumnIndexOrThrow(AnddosDbAdapter.colAccountUsername));
        String password = account.getString(account.getColumnIndexOrThrow(AnddosDbAdapter.colAccountPassword));
        String service = account.getString(account.getColumnIndexOrThrow(AnddosDbAdapter.colAccountService));
        textUsernameDetail.setText(username);
        textServiceDetail.setText(service);
        
        
        Account accRapid = new Account();
        accRapid.setService(service);
        accRapid.setUserName(username);
        accRapid.setPassword(password);
        
        dbAdapter.close();
        // nefunguje - nekdo smayal account tridu z main package jirka to musi predelat na account tridu z db
        
        try {
			ServiceProvider serviceProvider = ServiceProviderFactory.getProvider(accRapid);
			AccountDetail accountDetail = serviceProvider.getAccountDetail(accRapid);
			if(accountDetail != null){
				
				
				String email;
				if(accountDetail.getEmail() == null)
					email = "";
				else
					email = accountDetail.getEmail();
				
				
				String billeduntil;
				Date billeduntilDate = accountDetail.getBilledUntil();
				if(billeduntilDate == null)
					billeduntil = "";
				else
					billeduntil = billeduntilDate.toString();
				
				
				String currentspace = String.valueOf(accountDetail.getCurrentSpace());

				//extras != null ? extras.getLong(AnddosDbAdapter.colAccountID) : null;
				
				textEmailDetail.setText(email);
				textBilledUntilDetail.setText(billeduntil);
				textCurrentSpaceDetail.setText(currentspace);
			} else {
				Toast.makeText(this, "Bad account", Toast.LENGTH_SHORT).show();
			}
		} catch (MalformedURLException e) {
			Toast.makeText(this, "Error in Service provider ", Toast.LENGTH_SHORT).show();
		} catch (ServiceProviderException e) {
			Toast.makeText(this, "Error in get Account Detail  " + e.getMessage() , Toast.LENGTH_SHORT).show();
			Log.w("", e.toString());
		}
        
        
        //dialog.dismiss();
            
//            mUsernameText.setEnabled(false);
//            mPasswordText.setEnabled(false);
//            mServiceSpinner.setEnabled(false);
            
            //mPasswordText.setText(account.getString(account.getColumnIndexOrThrow(AnddosDbAdapter.colAccountPassword)));
            
            
                        
      
    }
	
	 

}

